package com.shanjupay.merchant.controller;

import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.utils.SecurityUtil;
import com.shanjupay.transaction.api.PayChannelService;
import com.shanjupay.transaction.api.dto.PayChannelDTO;
import com.shanjupay.transaction.api.dto.PayChannelParamDTO;
import com.shanjupay.transaction.api.dto.PlatformChannelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: shanjupay
 * @description:平台支付参数相关的controller
 * @author: Mr.Hkc
 * @create: 2020-08-19 14:34
 **/
@Api(value = "商户平台‐渠道和支付参数相关", tags = "商户平台‐渠道和支付参数", description = "商户平台‐渠道和支付参数相关")
@RestController
public class PlatformParamController {

    @Reference
    PayChannelService payChannelService;


    @ApiOperation("获得指定应用指定服务类型所包含的原始支付渠道参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用id", name = "appId", dataType = "String", paramType = "path"),
            @ApiImplicitParam(value = "服务类型代码", name = "platformChannel", dataType = "String", paramType = "path")
    })
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public List<PayChannelParamDTO> queryPayChannelParam(@PathVariable(value = "appId") String appId, @PathVariable(value = "platformChannel") String platformChannel) {
        return payChannelService.queryPayChannelParamByAppAndPlatform(appId, platformChannel);
    }


    @ApiOperation("获得单个指定应用指定服务类型所包含的原始支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用id", name = "appId", dataType = "String", paramType = "path"),
            @ApiImplicitParam(value = "服务类型代码", name = "platformChannel", dataType = "String", paramType = "path"),
            @ApiImplicitParam(value = "实际支付渠道编码", name = "payChannel", dataType = "String", paramType = "path")
    })
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public PayChannelParamDTO queryParamByAppPlatformAndPayChannel(@PathVariable(value = "appId") String appId,
                                                                         @PathVariable(value = "platformChannel") String platformChannel,
                                                                         @PathVariable(value = "payChannel") String payChannel) {
        return payChannelService.queryParamByAppPlatformAndPayChannel(appId, platformChannel,payChannel);
    }


    @ApiOperation("商户配置支付渠道参数")
    @ApiImplicitParam(name = "payChannelParamDTO", value = "支付渠道参数", required = true, dataType = "PayChannelParamDTO", paramType = "body")
    @RequestMapping(value = "/my/pay-channel-params",method = {RequestMethod.POST,RequestMethod.PUT})
    void createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParamDTO){
        if(payChannelParamDTO == null || payChannelParamDTO.getChannelName() == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        //商户id
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParamDTO.setMerchantId(merchantId);
        payChannelService.savePayChannelParam(payChannelParamDTO);
    }


    @ApiOperation("根据平台服务类型获取支付渠道列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "platformChannelCode",
            value = "服务类型编码", required = true,
            dataType = "String", paramType = "path")
    })
    @GetMapping(value = "/my/pay-channels/platform-channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable(value = "platformChannelCode") String platformChannelCode) {
        return payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
    }


    /**
     * 查询所有平台服务类型
     *
     * @return
     */
    @ApiOperation("获取平台服务类型")
    @GetMapping(value="/my/platform-channels")
    public List<PlatformChannelDTO> queryPlatformChannel() {
        return payChannelService.queryPlatformChannel();
    }

    @ApiOperation("绑定服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用id", name = "appId", dataType = "string", paramType = "path"),
            @ApiImplicitParam(value = "服务类型code", name = "platformChannelCodes", dataType = "string", paramType = "query")
    })
    @PostMapping(value = "/my/apps/{appId}/platform-channels")
    public void bindPlatformForApp(@PathVariable(value = "appId") String appId,
                                   @RequestParam("platformChannelCodes") String platformChannelCodes) {
        payChannelService.bindPlatformChannelForApp(appId, platformChannelCodes);
    }

    @ApiOperation("查询用户是否已经绑定了某个服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用id", name = "appId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "服务类型", name = "platformChannel", dataType = "string", paramType = "query")
    })
    @GetMapping(value = "/my/merchants/apps/platformchannels")
    public int queryAppBindPlatformChannel(@RequestParam(value = "appId") String appId,
                                           @RequestParam(value = "platformChannel") String platformChannel) {
        return payChannelService.queryAppBindPlatformChannel(appId, platformChannel);
    }


}

